package com.xkcyy.one.controller;

import com.xkcyy.one.common.Result;
import com.xkcyy.one.dto.UserDTO;
import com.xkcyy.one.entity.User;
import com.xkcyy.one.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author AI Assistant
 */
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    private final UserService userService;
    
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    
    /**
     * 获取所有用户
     *
     * @return 用户列表
     */
    @GetMapping("/list")
    public Result<List<UserDTO>> getAllUsers() {
        return Result.success(userService.getAllUsers());
    }
    
    /**
     * 根据ID获取用户
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/get")
    public Result<UserDTO> getUserById(@RequestParam Long id) {
        return Result.success(userService.getUserById(id));
    }

    /**
     * 根据用户名获取用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping("/username")
    public Result<UserDTO> getUserByUsername(@RequestParam String username) {
        return Result.success(userService.getUserByUsername(username));
    }
    
    /**
     * 创建用户
     *
     * @param user 用户信息
     * @return 创建后的用户信息
     */
    @PostMapping("/create")
    public Result<UserDTO> createUser(@RequestBody User user) {
        return Result.success(userService.createUser(user));
    }
    
    /**
     * 更新用户
     *
     * @param id   用户ID
     * @param user 用户信息
     * @return 更新后的用户信息
     */
    @PutMapping("/update")
    public Result<UserDTO> updateUser(@RequestParam Long id, @RequestBody User user) {
        user.setId(id);
        return Result.success(userService.updateUser(user));
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteUser(@RequestParam Long id) {
        userService.deleteUser(id);
        return Result.success();
    }
}
